//
//  YRRuntime.swift
//  Merchant
//
//  Created by 薛永伟 on 2019/5/22.
//  Copyright © 2019 Yuri. All rights reserved.
//

import UIKit

private var FirstAppearKey = "UIViewControlleriFirstAppearKey"
private var VCAppearTimesKey = "UIViewControlleriAppearTimesKey"


/// 运行时
class YRRuntime {
    
    /// 开启方法交换(⚠️注意：这里可能有意外的效果触发，所以一定要看看都修改了什么)
    class func letsSwizzle(){
        
        UIViewController.yrSwizzle()
        
        UIFont.yrSwizzle()
    }
    
}

//MARK: - --------------UIViewController--------------
extension UIViewController {
    
    @objc func newViewDidAppear(_ animated: Bool) {
        
        newViewDidAppear(animated)
        appearTimes += 1
    }
    
    /// 开启交换方法
    class func yrSwizzle() {
        
        //Make sure This isn't a subclass of UIViewController, So that It applies to all UIViewController childs
        if self != UIViewController.self {
            return
        }
        
        let _: () = {
            let originalSelector = #selector(UIViewController.viewDidAppear(_:))
            let swizzledSelector = #selector(UIViewController.newViewDidAppear(_:))
            let originalMethod = class_getInstanceMethod(self, originalSelector)
            let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
            method_exchangeImplementations(originalMethod!, swizzledMethod!);
        }()
    }
    
    /// 第一次被展示
    var isFirstAppear: Bool{
        
        return appearTimes == 1
    }
   
    /// 出现次数
    private(set) var appearTimes:Int {
        
        set{
            objc_setAssociatedObject(self, &VCAppearTimesKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
        }
        
        get{
            if let reslut = objc_getAssociatedObject(self, &VCAppearTimesKey) as? Int {
                return reslut
            }
            return 0
        }
    }
    
}


//MARK: - --------------UIFont--------------

extension UIFont {
    
    /// 设置字体按照比例进行缩放
    @objc class func newSystemFont(ofSize:CGFloat,weight:CGFloat){
        
        newSystemFont(ofSize: ofSize.scaleLength, weight: weight)
    }
    
    /// 开启交换方法
    class func yrSwizzle() {
        
        //Make sure This isn't a subclass of UIViewController, So that It applies to all UIViewController childs
        if self != UIFont.self {
            return
        }
        
        let _: () = {
            let originalSelector = #selector(UIFont.systemFont(ofSize:weight:))
            let swizzledSelector = #selector(UIFont.newSystemFont(ofSize:weight:))
            
            let originalMethod = class_getClassMethod(self, originalSelector)
            let swizzledMethod = class_getClassMethod(self, swizzledSelector)
            method_exchangeImplementations(originalMethod!, swizzledMethod!);
        }()
    }
}

//MARK: - --------------UILabel--------------
private var LabelFitFontSizeKey = "UILabelFitFontSizeKey"

extension UILabel {
    
    @IBInspectable var fitFontSize: CGFloat {
        
        set {
            font = font.withSize(fitFontSize.scaleLength)
            objc_setAssociatedObject(self, &LabelFitFontSizeKey, newValue, .OBJC_ASSOCIATION_ASSIGN)
        }
        get {
            
            if let fs = objc_getAssociatedObject(self, &LabelFitFontSizeKey) as? CGFloat {
                return fs
            }
            return font.pointSize
        }
    }
    
}

//MARK: - --------------UIButton--------------

private var ButtonFitFontSizeKey = "UIButtonFitFontSizeKey"

extension UIButton {
    
    @IBInspectable var fitFontSize: CGFloat {
        
        set {
            if let font = titleLabel?.font {
                titleLabel?.font = font.withSize(fitFontSize.scaleLength)
                objc_setAssociatedObject(self, &LabelFitFontSizeKey, newValue, .OBJC_ASSOCIATION_ASSIGN)
            }
        }
        get {
            
            if let fs = objc_getAssociatedObject(self, &LabelFitFontSizeKey) as? CGFloat {
                return fs
            }
            return titleLabel?.font.pointSize ?? 1.0
        }
    }
    
}
